/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dodatki;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.BASE64Encoder;

/**
 * Klasa pozwalajaca na kodowanie ciągów danych algorytmem SHA
 * @author Grzesiek
 */
public class SHA {

/**
 * Wyliczanie skrótu z podanego ciągu wejściowego
 * @param str ciąg znaków do zakodowania
 * @return
 */
    public static String zakoduj(String str) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(SHA.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            md.update(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(SHA.class.getName()).log(Level.SEVERE, null, ex);
        }


        byte raw[] = md.digest();
        String hash = (new BASE64Encoder()).encode(raw);
        return hash; 
    }
    
}
